Skip to content

solver: contract states fetched and verified at the head instead of re-executed - #95

Merged
EnzinoBB merged 8 commits into
masterfrom
feat/contract-state-fetch
Oct 24, 2026
Merged

EnzinoBB merged 8 commits into
masterfrom
feat/contract-state-fetch

Conversation

@EnzinoBB

Copy link
Copy Markdown
Collaborator

Steps 2–3 of the contract state sync plan (docs/specs/2026-10-24-contract-state-sync-design.md on docs/contract-state-sync). Built on #94 (step 1, fee attribution), which already contains #93 (the dice scenario).

Work in progress:

  • step 2 (done): each contract records the call and the hash of the last state the chain declares, whether or not the node obtained it, so the node knows which states it holds stale. The fields are appended to the checkpoint format and are backward compatible.
  • step 3 (to do): stale states are fetched from the confidants once the node is live, and accepted only when their call and hash are the declared ones. This also closes the case where a reply was accepted unverified when its block was not available.

🤖 Generated with Claude Code

EnzinoBB and others added 7 commits October 24, 2026 16:38
Each contract's StateItem keeps the call and the hash of the last
new_state with a state (declared_ref, declared_hash), whether or not this
node obtained the state: a node knows which contracts it holds stale
(is_stale, count_stale) and can verify a state from a peer without the
block. Serialized after the existing fields (checkpoints of earlier
builds load without them; the quick-start validation fills them from the
chain when the block is still stored and logs how many are stale).

Step 2 of the contract state sync plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ified

Once the node is live (the first live block after read ones), every
contract whose state is not the one the chain declares is asked for from
the confidants with the existing StateRequest, at most 16 a round,
oldest first, each one again after 50, 100, ... up to 5000 rounds. A
reply is taken only for the declared call with the declared hash
(newer, older or other states are ignored), and the cache is overwritten
(a re-execution may have stored a wrong state for the same call). A
contract without a declaration (a checkpoint of an earlier build) is
checked against its block as before, but no longer taken unverified
when the block is not stored; a state of an unknown contract is refused.
The node logs each verified state and a summary of known, stale and
pending contracts when it becomes live and every 1000 rounds.

contracts-dice-join is no longer manual: the joining node must end with
the same balances and Dice state as the live nodes.

Step 3 of the contract state sync plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- StateItem bytes gain the declaration only when one is known, so an
  item of a checkpoint from an earlier build serializes to the same
  bytes and the checkpoint's hashes still match: before, every such
  checkpoint was quarantined at the first start of the new build.
- Removing a block whose new_state declared a contract's state moves the
  declaration back to the latest successful new_state left on the chain
  (unknown without one): the restored state was otherwise stale for
  good and every peer reply looked older.
- A state of a contract this node does not know ends its requests.
- Test: an item without a declaration has the earlier layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EnzinoBB
EnzinoBB marked this pull request as ready for review October 24, 2026 18:36
@EnzinoBB
EnzinoBB merged commit 8d2e580 into master Oct 24, 2026
2 checks passed
EnzinoBB added a commit that referenced this pull request Nov 1, 2026
…inst the chain

A node that replays contract calls while syncing cannot reproduce every execution (a contract
whose result depends on its own balance, for instance). Such a contract's state stays stale, the
node never obtains the state the chain declares, and its later executions start from a wrong
state. This change lets the node notice and repair that.

- each contract's StateItem records the call and the hash of the last new_state with a state
  (declared_ref, declared_hash), whether or not the node obtained the state, so a node knows which
  contracts it holds stale (is_stale, count_stale). The declaration is serialized after the
  existing fields only when one is known: an item of an earlier checkpoint keeps its bytes and its
  hash; the quick-start validation fills it from the chain when the block is stored
- once the node is live (the first live block after read ones), every stale contract is asked for
  from the confidants with the existing StateRequest (StateFetcher, solver/statefetcher.hpp): at most
  16 a round, oldest first, each again after 50, 100, ... up to 5000 rounds
- a StateReply is taken only for a contract this node asked for, and only for the declared call
  with the declared hash (newer, older or other states are ignored); the cache is overwritten (a
  re-execution may have stored a wrong state for the same call). A contract without a declaration
  is checked against its block as before, but no longer taken unverified when the block is not
  stored; a state of an unknown contract is refused
- removing a block moves the declaration back to the latest successful new_state left on the chain
- get_actual_state warns when a state the block declares is not available
- [params] sync_contract_execution (default true, the current behaviour): false skips the
  re-execution of contract calls while the node reads the blockchain; every state is then fetched
  from the confidants once the node is live, and syncing needs no executor
- unit tests in tests/csnode/smartcontracts_tests.cpp (state bookkeeping, earlier layout, the reply
  check)

Local: the request and reply messages are the existing ones, and a node only changes what it
accepts. It builds on the PR that charges emitted transactions to the latest execution (fee rule).

Same change in the community fork: #95, #96 (and the warning of #83).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant